import apiRequest from '@/utils/apiRequest';

/**
 * 获取供应商列表
 * @param {Object} params - 查询参数
 */
export const getSuppliers = async (params) => {
  try {
    const response = await apiRequest.get('/api/Suppliers', { params });
    return response.data;
  } catch (error) {
    console.error('Failed to get suppliers:', error);
    throw error;
  }
};

/**
 * 获取供应商详情
 * @param {string} supplierCode - 供应商编码
 */
export const getSupplier = async (supplierCode) => {
  try {
    const response = await apiRequest.get(`/api/Suppliers/${supplierCode}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get supplier ${supplierCode}:`, error);
    throw error;
  }
};

/**
 * 创建供应商
 * @param {Object} supplierData - 供应商数据
 */
export const createSupplier = async (supplierData) => {
  try {
    const response = await apiRequest.post('/api/Suppliers', supplierData);
    return response.data;
  } catch (error) {
    console.error('Failed to create supplier:', error);
    throw error;
  }
};

/**
 * 更新供应商信息
 * @param {string} supplierCode - 供应商编码
 * @param {Object} supplierData - 供应商数据
 */
export const updateSupplier = async (supplierCode, supplierData) => {
  try {
    const response = await apiRequest.put(`/api/Suppliers/${supplierCode}`, supplierData);
    return response.data;
  } catch (error) {
    console.error(`Failed to update supplier ${supplierCode}:`, error);
    throw error;
  }
};

/**
 * 删除供应商
 * @param {string} supplierCode - 供应商编码
 */
export const deleteSupplier = async (supplierCode) => {
  try {
    const response = await apiRequest.delete(`/api/Suppliers/${supplierCode}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to delete supplier ${supplierCode}:`, error);
    throw error;
  }
};

/**
 * 更新供应商状态
 * @param {string} supplierCode - 供应商编码
 * @param {string} status - 供应商状态 ('启用' 或 '禁用')
 */
export const updateSupplierStatus = async (supplierCode, status) => {
  try {
    const response = await apiRequest.patch(`/api/Suppliers/${supplierCode}/status`, {
      status
    });
    return response.data;
  } catch (error) {
    console.error(`Failed to update status for supplier ${supplierCode}:`, error);
    throw error;
  }
};